/*
 * Author   Code        Date            Remark
 * cd       00001       2017-05-15      蹇€熶笅鍗曞鍔犵墿娴佸叕鍙搁€夋嫨
 * whz 		whz20170724 2017-07-24                 鎼滅储妗嗗幓鎺夊墠鍚庣┖鏍�
 * xwk      00002       2017-07-25      鍔犲叆璐墿杞﹀悗锛屽鏋滄槸鍒嗗叕鍙哥殑鍟嗗搧淇濊瘉鏁伴噺鑳芥甯告樉绀�
 */
 

		
		
		
		/*鍚嶇О鎼滅储鏉′欢鍥炶溅瑙﹀彂鏌ヨ*/
		$('#arg_name').keydown(function(e){
	    	var key = e.which;
            if (key == 13) {		
                e.preventDefault();
            	search(1);
            }
		});
		
		//djf0001
		function search(type) {
			var isqh="";
			if($('#chkqh').is(':checked')) {
				isqh+="&isqh=1";
			}else{
				isqh+="&isqh=0";
			}
			if(type==1) {
				//alert('----'+$("#arg_name").val()+'----'+$.trim($("#arg_name").val())+'-----');
				//whz20170724
				var arg_name = $.trim($("#arg_name").val());
				arg_name = encodeURIComponent(arg_name);
				var s_jg = $("#s_jg").val();
				var e_jg = $("#e_jg").val();
				window.location = "quoteList.action?km="+urlkm+"&pp="+urlpp+"&network="+urlnetwork+"&arg_name="+arg_name+"&s_jg="+s_jg+"&e_jg="+e_jg+"&tykhgsdm="+tykhgsdm+isqh;
			}else if(type==2) {	/*鏂版*/
				window.location = "quoteList.action?type=2&tykhgsdm="+tykhgsdm+isqh;
			}else if(type==3){	/*鎺ㄨ崘*/
				window.location = "quoteList.action?type=3&tykhgsdm="+tykhgsdm+isqh;
			}else {
				window.location = "quoteList.action?type=4&tykhgsdm="+tykhgsdm+isqh;
			}
		}		
		
		function searchByRange(sjg,ejg) {
			var arg_name = $("#arg_name").val();
			arg_name = encodeURIComponent(arg_name);
			var s_jg = sjg;
			var e_jg = ejg;
			var loc = "quoteList.action?km="+urlkm+"&pp="+urlpp+"&network="+urlnetwork+"&arg_name="+arg_name+"&tykhgsdm="+tykhgsdm;
			if(s_jg!=null){
				loc=loc+"&s_jg="+s_jg;
			}
			if(e_jg!=null){
				loc=loc+"&e_jg="+e_jg;
			}
			window.location = loc;
		}
		
		function jump(url,km,pp,network){
			url=url+'&type='+type+"&tykhgsdm="+tykhgsdm+"&km="+encodeURIComponent(km)+"&pp="+encodeURIComponent(pp)+"&network="+encodeURIComponent(network);
			window.location=url;
		}

		function kmjump(pp,km,network) {
			pp = encodeURIComponent(pp);
			km = encodeURIComponent(km);
			network = encodeURIComponent(network);
			window.location="quoteList.action?pp="+pp+"&km="+km+"&network="+network+"&tykhgsdm="+tykhgsdm
		}
		function networkjump(gsdm,tykhgsdm,km,pp,network) {
			pp = encodeURIComponent(pp);
			km = encodeURIComponent(km);
			network = encodeURIComponent(network);
			window.location="quoteList.action?gsdm="+gsdm+"&km="+km+"&pp="+pp+"&network="+network+"&tykhgsdm="+tykhgsdm
		}	
		
		/*娣诲姞璐墿杞�*/
		function addCart(e,spid,gsdm,fullname,jg,s_sl,b2b_ddlx) {
			try {
				var v_sl = document.getElementById(s_sl).value;
				if(v_sl == undefined || v_sl == "") {
					v_sl = 1;
				}
			}catch(e) {
				//鍑虹幇寮傚父锛屾暟閲忛粯璁�1
				v_sl = 1;
			}
			$.ajax({
				url:"addCart.action",
				async:true,
				cache:false,
				dataType:"json",
				success:function(response) {
					
					if(response.ret == "0") {
						var gwx=$(".chart_btu").offset().left;
						var gwy=$(".chart_btu").offset().top;
						//00002	
						var addx=$('#a_gm'+gsdm+'_'+spid).offset().left;
		                var addy=$('#a_gm'+gsdm+'_'+spid).offset().top;
						var cnt=$("#"+s_sl).val();
						if(cnt!=''){
							$("body").append('<font class="buy_add2">+'+cnt+'</font>');
							$(".buy_add2").css({"left":addx-100,"top":addy});
						}else{
							$("body").append('<font class="buy_add2">+1</font>');
							$(".buy_add2").css({"left":addx-100,"top":addy});
						}
						$(".buy_add2").animate({top:gwy-100,left:gwx,opacity:"0"},1000);
						$('#gwcsl').html(response.count);
						//00002
						$('#a_gm'+gsdm+'_'+spid).html("璐�("+response.c_item+")");
//						$('#hot_gm'+spid).html("璐�("+response.c_item+")");
						cnt=response.c_item;
					}else {
						alert(response.msg);
					}
				},
				error:function(xhr,errinfo,o) {
					alert(errinfo)
				},
				timeout:30000,
				type:"post",
				data:{gsdm:gsdm,sptid:spid,spmc:fullname,jg:jg,spsl:v_sl,b2bddlx:b2b_ddlx,type:'quote'}
			})					
		}

		/*娣诲姞璐墿杞�*/
		function hotaddCart(e,spid,gsdm,fullname,jg,s_sl,b2b_ddlx) {
			try {
				var v_sl = document.getElementById(s_sl).value;
				if(v_sl == undefined || v_sl == "") {
					v_sl = 1;
				}
			}catch(e) {
				//鍑虹幇寮傚父锛屾暟閲忛粯璁�1
				v_sl = 1;
			}
			$.ajax({
				url:"addCart.action",
				async:true,
				cache:false,
				dataType:"json",
				success:function(response) {
					
					if(response.ret == "0") {
						var gwx=$(".chart_btu").offset().left;
						var gwy=$(".chart_btu").offset().top;	
						var addx=$('#hot_gm'+spid).offset().left;
		                var addy=$('#hot_gm'+spid).offset().top;
						var cnt=$("#"+s_sl).val();
						if(cnt!=''){
							$("body").append('<font class="buy_add2">+'+cnt+'</font>');
							$(".buy_add2").css({"left":addx-100,"top":addy});
						}else{
							$("body").append('<font class="buy_add2">+1</font>');
							$(".buy_add2").css({"left":addx-100,"top":addy});
						}
						$(".buy_add2").animate({top:gwy-100,left:gwx,opacity:"0"},1000);
						$('#gwcsl').html(response.count);
						$('#hot_gm'+spid).html("璐�("+response.c_item+")");
						//00002
						$('#a_gm'+gsdm+'_'+spid).html("璐�("+response.c_item+")");
						cnt=response.c_item;
					}else {
						alert(response.msg);
					}
				},
				error:function(xhr,errinfo,o) {
					alert(errinfo)
				},
				timeout:30000,
				type:"post",
				data:{gsdm:gsdm,sptid:spid,spmc:fullname,jg:jg,spsl:v_sl,b2bddlx:b2b_ddlx,type:'quote'}
			})					
		}
		
	function countjg(td_spzj,td_spjg,text_sl,gsdm,spid,fullname) {
		var re =new RegExp("^[0-9]+$")
		var old_spzj = document.getElementById(td_spzj).innerHTML.replace(/,/g,'');	/*鍙栧緱褰撳墠鍟嗗搧鐨勬€讳环鏍�*/
		var spjg = document.getElementById(td_spjg).innerHTML;	/*鑾峰彇鍟嗗搧鍗曚环*/
		if(re.test(text_sl.value)) {
			$.ajax({
				url:"addLadderCart.action",
				async:true,
				cache:true,
				dataType:"json",
				success:function(response) {
					if(response.ret == "0") {
			
						var spzj = parseInt(text_sl.value) * spjg.replace(/,/g,'');
						document.getElementById(td_spzj).innerHTML = spzj;
						var zjg = document.getElementById('countjg').innerHTML.replace(/,/g,'');
						var cjg=parseFloat(zjg) + parseFloat(spzj) - parseFloat(old_spzj);
						document.getElementById('countjg').innerHTML = cjg.toFixed(2);	/*鏇存柊璐墿杞︽墍鏈夊晢鍝佹€讳环鏍�*/
					}
				},
				error:function(xhr,errinfo,o) {
					alert(errinfo)
				},
				timeout:30000,
				type:"post",
				data:{gsdm:gsdm,sptid:spid.replace(/,/g,''),spmc:fullname,jg:spjg,spsl:text_sl.value}
			})			
		}
	}	
	
	
		/*蹇€熶笅鍗曟ā鎬佸璇濈獥鍙�*/
		$('#quickOrderModal').on('show.bs.modal', function (event) {
		  	var modal = $(this);
	  		$.ajax({
				url:"openQuickOrderView.action",
				async:false,
				cache:false,
				dataType:"json",
				success:function(response) {
					$('#quickOrderTable').html(response.cart);
					var $input = $('#txt_spmc_slt');
					$input.typeahead({
					source:response.quote, 
					autoSelect: true,
					items:'15',
					scrollHeight:0,
					displayText:function(item){
						return item.fullname || item;
					},afterSelect:function(item){
						var $target = $("#div_qo").find("input[isActive='1']");
						$target.eq(0).focus();
					},matcher: function (item) {	/*鎵╁睍鎷奸煶棣栧瓧姣�*/
					    var it = this.displayText(item);
					    var it_py = item.fullname_py || item;	
					    var query_arr = this.query.split(' ');
					    var r = 0;
					    for(var i=0;i<query_arr.length;i++) {
						    r = ~it.toLowerCase().indexOf(query_arr[i].toLowerCase()) || ~it_py.toLowerCase().indexOf(query_arr[i].toLowerCase());
						    if(r == 0) {
						    	return r;
						    }
					    }
					    //var r = ~it.toLowerCase().indexOf(this.query.toLowerCase()) || ~it_py.toLowerCase().indexOf(this.query.toLowerCase());
					    return r;
					}}); 
					$input.change(function() {
					    var current = $input.typeahead("getActive");
					    if (current) {
					        if (current.fullname == $input.val()) {
								if (khjb == '1') {
					        		$("#spdj").val(current.jg2);
								}else if (khjb == '2') {
					        		$("#spdj").val(current.jg4);
								}else if (khjb == '3') {
					        		$("#spdj").val(current.jg5);
								}else if (khjb == '4') {
					        		$("#spdj").val(current.jg6);
								}else if (khjb == '5') {
					        		$("#spdj").val(current.jg7);
								}			        
								$("#txt_spmc_id").val(current.sptid);
								$("#txt_gsdm").val(current.gsdm);
					        } else {
					        }
					    } else {
					    }
					});		
				},
				error:function(xhr,errinfo,o) {
					alert(errinfo)
				},
				timeout:30000,
				type:"post",
				data:{}
			});		
			
		});	
		
		/*蹇€熶笅鍗曟ā鎬佹鏄剧ず瀹屾垚锛堝苟涓斿悓鏃跺湪 CSS 杩囨浮鏁堟灉瀹屾垚锛変箣鍚庤瑙﹀彂*/
		$('#quickOrderModal').on('shown.bs.modal', function (event) {
			$('#txt_spmc_slt').val('');
			$('#txt_spmc_id').val('');
			$("#spsl").val('');
			$("#spdj").val('');
			$("#zje").val('');
			
			document.getElementById('txt_spmc_slt').focus();	/*灏嗙劍鐐硅缃湪鍟嗗搧鍚嶇О鏌ヨ杈撳叆妗�*/
		});
		
		/*寮€鎵撳揩閫熶笅鍗曞璇濈獥鍙�*/
		function openQuickOrder() {
			$("#quickOrderModal").modal('show');
		}
		
		/*娣诲姞蹇€熶笅鍗曡喘鐗╄溅*/
		function addQuickOrderCart() {
			var reint =new RegExp("^[0-9]+$")
			var refloat =new RegExp("^[0-9]+|[0-9]+.[0-9]+$")
			var txt_spmc_id = $("#txt_spmc_id").val();
			var spsl = $("#spsl").val();
			var spdj = $("#spdj").val();
			var txt_spmc_slt = $("#txt_spmc_slt").val();
			var txt_gsdm = $("#txt_gsdm").val();
			if(txt_spmc_id == "") {
				alert("璇疯緭鍏ヨ璐晢鍝�");
				return;
			}
			if(!reint.test(spsl)) {
				alert("璇疯緭鍏ユ纭殑鍟嗗搧鏁伴噺");
				return;
			}
			if(!refloat.test(spdj)) {
				alert("鍟嗗搧鍗曚环涓嶆纭�");
				return;
			}

			$.ajax({
				url:"addQOCart.action",
				async:false,
				cache:false,
				dataType:"json",
				success:function(response) {
					if(response.dataCode.code == 0) {
						$('#quickOrderTable').html(response.data.split("$$$")[0]);
						$("#txt_spmc_id").val('');
						$("#txt_spmc_slt").val('');
						$("#txt_gsdm").val('');
						$("#spsl").val('');
						$("#spdj").val('');
						$("#zje").val('');
						document.getElementById('txt_spmc_slt').focus();
						$('#gwcsl').html(response.data.split("$$$")[1]);
					}else {
						alert(response.dataCode.message)
					}
				},
				error:function(xhr,errinfo,o) {
					alert(errinfo)
				},
				timeout:30000,
				type:"post",
				data:{type:'add',sptid:txt_spmc_id,spmc:txt_spmc_slt,jg:spdj,spsl:spsl,cxflag:'0',gsdm:txt_gsdm,isladder:'1'}
			});	
		
		}
		
		/*蹇€熶笅鍗曟彁浜よ鍗�*/
		$('#quickOrderModalForm').ajaxForm({
			beforeSubmit:function() {
				var shr = $("#shr").val();
				var shrsj = $("#shrsj").val();
				var shrdz = $("#shrdz").val();
				
				if(shr == "") {
					alert("璇烽€夋嫨鍦板潃锛屾垨鑰呰緭鍏ユ敹璐т汉濮撳悕");
					return false;
				}
				if(shrsj == "") {
					alert("璇烽€夋嫨鍦板潃锛屾垨鑰呰緭鍏ユ敹璐т汉鐢佃瘽");
					return false;
				}
				if(shrdz == "") {
					alert("璇烽€夋嫨鍦板潃锛屾垨鑰呰緭鍏ユ敹璐т汉鍦板潃");
					return false;
				}
				$("#ksxd").css('display','none'); 
				return true;
			},
			success:function(r) {
				if(r.dataCode.code == 0) {
					alert('蹇€熶笅鍗曟垚鍔�');
					$("#txt_spmc_id").val('');
					$("#txt_spmc_slt").val('');
					$("#spsl").val('');
					$("#spdj").val('');
					$("#zje").val('');
					$("#shr").val('');
					$("#shrsj").val('');
					$("#shrdz").val('');
					$("#slt_shrdz").val('璇烽€夋嫨鍦板潃');
					// 00001
					$("#slt_wlgs").val('璇烽€夋嫨鐗╂祦鍏徃');
					$("#quickOrderTable").html('');
					
					document.getElementById('txt_spmc_slt').focus();
				}else if(r.dataCode.code == -4) {
					alert(r.dataCode.message)
				}else {
					alert('蹇€熶笅鍗曞け璐�');
				}
				$("#ksxd").css('display',''); 
			}
		});		
		/*鍒犻櫎蹇€熶笅鍗曞晢鍝�*/
		function delQuickConfirm(cartid,cartitemid) {
			$.ajax({
				url:"delCart.action",
				async:false,
				cache:false,
				dataType:"json",
				success:function(response) {
					$("#quickOrderTable").html(response.split("$$$")[0]);
					$('#gwcsl').html(response.split("$$$")[1]);
				},
				error:function(xhr,errinfo,o) {
					alert(errinfo)
				},
				timeout:30000,
				type:"post",
				data:{type:'quick',cartid:cartid,cartItemId:cartitemid}
			});	
		}	

	window.onload=function(){
		$(window).trigger("scroll");
	};
	$(window).resize(function(){
		$(window).trigger("scroll");	
	});
	
	function goPage(page){
		var arg_name = $("#arg_name").val();
		arg_name = encodeURIComponent(arg_name);
		var s_jg = $("#s_jg").val();
		var e_jg = $("#e_jg").val();
		window.location = "quoteList.action?gsdm="+gsdm+"&km="+urlkm+"&pp="+urlpp+"&network="+urlnetwork+"&arg_name="+arg_name+"&s_jg="+s_jg+"&e_jg="+e_jg+"&currentPage="+page+"&type="+type+"&tykhgsdm="+tykhgsdm;
	}
